//
//  SDAlertManagement.m
//  SKinDiary
//
//  Created by pcyang on 16/6/26.
//  Copyright © 2016年 tencent. All rights reserved.
//

#import "SDAlertManagement.h"
#import "VPMProgressHUD.h"

@interface SDImageAlertView : UIView
@property (nonatomic,strong) UIImageView* bgImageView;
@property (nonatomic,strong) UILabel* tipLabel;
@property (nonatomic,strong) UIImageView* tipImageView;
@end

@implementation SDImageAlertView
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if ( self )
    {
        self.frame = CGRectMake(0, 0, 130, 130);
        UIImage *image = SDGetImageByName(@"alertview_background_img");
        self.bgImageView = [[UIImageView alloc]initWithImage:image];
        [self addSubview:self.bgImageView];
        
        self.tipImageView = [[UIImageView alloc]initWithFrame:CGRectMake(40, 10, 50, 50)];
        self.tipImageView.contentMode = UIViewContentModeCenter;
        [self addSubview:self.tipImageView];
        
        self.tipLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 70, 130, 60)];
        self.tipLabel.backgroundColor = [UIColor clearColor];
        self.tipLabel.numberOfLines = 0;
        self.tipLabel.textColor = [UIColor whiteColor];
        self.tipLabel.font = [UIFont systemFontOfSize:16];
        self.tipLabel.textAlignment = NSTextAlignmentCenter;
        [self addSubview:self.tipLabel];
    }
    return self;
}

- (void)showAlertWithImage:(UIImage*)image text:(NSString*)text
{
    self.tipImageView.image = image;
    self.tipLabel.text = text;
    
    UIWindow* keyWindow = [UIApplication sharedApplication].keyWindow;
    
    self.center = CGPointMake(keyWindow.frame.size.width/2, keyWindow.frame.size.height/2);
    self.alpha = 0.0;
    
    [keyWindow addSubview:self];
    
    [UIView animateWithDuration:.25 animations:^{
        self.alpha = 1.0;
    }completion:^(BOOL finished) {
        [UIView animateWithDuration:.25 delay:1.5 options:0 animations:^{
            self.alpha = 0.0;
        } completion:^(BOOL finished) {
            [self removeFromSuperview];
        }];
    }];
}


- (void)showAlertWithText:(NSString*)text parentView:(UIView*) parentView completion:(void (^)(BOOL finished))completion
{
    self.tipImageView = nil;
    self.tipLabel.bounds = CGRectMake(0, 0, 210, 50);
    self.tipLabel.text = text;
    [self.tipLabel sizeToFit];
    self.tipLabel.center = CGPointMake(self.bounds.size.width/2.f, self.bounds.size.height/2.f);
    
    self.center = CGPointMake(parentView.bounds.size.width/2, parentView.bounds.size.height/2);
    self.alpha = 0.0;
    
    [parentView addSubview:self];
    
    [UIView animateWithDuration:.25 animations:^{
        self.alpha = 1.0;
    }completion:^(BOOL finished) {
        [UIView animateWithDuration:.25 delay:3. options:0 animations:^{
            self.alpha = 0.0;
        } completion:^(BOOL finished) {
            [self removeFromSuperview];
            if(completion)
                completion(finished);
        }];
    }];
}

- (void)showAlertWithImage:(UIImage*)image text:(NSString*)text parentView:(UIView*) parentView completion:(void (^)(BOOL finished))completion
{
    self.tipImageView.image = image;
    self.tipLabel.text = text;
    
    self.center = CGPointMake(parentView.bounds.size.width/2, parentView.bounds.size.height/2);
    self.alpha = 0.0;
    
    [parentView addSubview:self];
    
    [UIView animateWithDuration:.25 animations:^{
        self.alpha = 1.0;
    }completion:^(BOOL finished) {
        [UIView animateWithDuration:.25 delay:1.5 options:0 animations:^{
            self.alpha = 0.0;
        } completion:^(BOOL finished) {
            [self removeFromSuperview];
            if(completion)
                completion(finished);
        }];
    }];
}

- (void)showAlertWithImage:(UIImage*)image text:(NSString*)text completion:(void (^)(BOOL finished))completion
{
    self.tipImageView.image = image;
    self.tipLabel.text = text;
    
    UIWindow* keyWindow = [UIApplication sharedApplication].keyWindow;
    
    self.center = CGPointMake(keyWindow.frame.size.width/2, keyWindow.frame.size.height/2);
    self.alpha = 0.0;
    
    [keyWindow addSubview:self];
    
    [UIView animateWithDuration:.25 animations:^{
        self.alpha = 1.0;
    }completion:^(BOOL finished) {
        [UIView animateWithDuration:.25 delay:1.5 options:0 animations:^{
            self.alpha = 0.0;
        } completion:^(BOOL finished) {
            [self removeFromSuperview];
            if(completion)
                completion(finished);
        }];
    }];
}

@end
@implementation SDAlertManagement
+ (void)showNetworkErrorAlert:(NSString*)des
{
    UIView* view = [VPMBProgressHUD  showAttendAlert:des andIcon:@""];
    view.userInteractionEnabled = NO;
}

+ (void)showNormalErrorAlert:(NSString*)alert
{
    [VPMBProgressHUD showAttendAlert:alert andIcon:@""];
}

+ (void)showDownloadImageSucessedAlertWithTransf:(CGAffineTransform) transf;
{
    [self showShareOKAlertWithTitle:@"下载图片成功" transf:transf];
}

+ (void)showDownloadImageFailedAlertWithTransf:(CGAffineTransform) transf
{
    [self showShareOKAlertWithTitle:@"下载图片失败" transf:transf];
}

+ (void)showShareOKAlertWithTitle:(NSString*)title transf:(CGAffineTransform) transf
{
    SDImageAlertView* alertView = [[SDImageAlertView alloc]initWithFrame:CGRectZero];
    alertView.transform = transf;
    //    [alertView showAlertWithImage:[UIImage staticImageWithName:@"BaseKit.bundle/alertview_icon_ok_img@2x.png"] text:title];
    UIImage *image = SDGetImageByName(@"alertview_icon_ok_img");
    [alertView showAlertWithImage:image  text:title];
}

+ (void)showShareOKAlertWithTitle:(NSString*)title
{
    SDImageAlertView* alertView = [[SDImageAlertView alloc]initWithFrame:CGRectZero];
    UIImage *image = SDGetImageByName(@"alertview_icon_ok_img");
    [alertView showAlertWithImage:image text:title];
}

+ (void)showShareErrorAlertWithTitle:(NSString*)title
{
    SDImageAlertView* alertView = [[SDImageAlertView alloc]initWithFrame:CGRectZero];
    
    UIImage *image = SDGetImageByName(@"alertview_icon_error_img");
    [alertView showAlertWithImage:image text:title];
    
}

+ (void)showShareWarnAlertWithTitle:(NSString*)title
{
    SDImageAlertView* alertView = [[SDImageAlertView alloc]initWithFrame:CGRectZero];
    UIImage *image = SDGetImageByName(@"alertview_icon_warn_img");
    [alertView showAlertWithImage:image text:title];
}

+ (UIView*)showShareWarnAlertWithTitle:(NSString*)title parentView:(UIView*) parentView completion:(void (^)(BOOL finished))completion
{
    SDImageAlertView* alertView = [[SDImageAlertView alloc]initWithFrame:CGRectZero];
    
    UIImage *image = SDGetImageByName(@"alertview_icon_warn_img");
    [alertView showAlertWithImage:image text:title parentView:parentView completion:completion];
    
    return alertView;
}

+ (void)showShareWarnAlertWithTitle:(NSString*)title transf:(CGAffineTransform) transf completion:(void (^)(BOOL finished))completion
{
    SDImageAlertView* alertView = [[SDImageAlertView alloc]initWithFrame:CGRectZero];
    alertView.transform = transf;
    UIImage *image = SDGetImageByName(@"alertview_icon_warn_img");
    [alertView showAlertWithImage:image text:title];
    
}

+ (void)showShareOKAlertWithTitle:(NSString*)title
                       parentView:(UIView*) parentView
                       completion:(void (^)(BOOL finished))completion;
{
    SDImageAlertView* alertView = [[SDImageAlertView alloc]initWithFrame:CGRectZero];
    UIImage *image = SDGetImageByName(@"alertview_icon_warn_img");
    [alertView showAlertWithImage:image text:title parentView:parentView completion:completion];
}

+ (void)showInfoWithTitle:(NSString*)title
               parentView:(UIView*) parentView
               completion:(void (^)(BOOL finished))completion
{
    SDImageAlertView* alertView = [[SDImageAlertView alloc]initWithFrame:CGRectZero];
    [alertView showAlertWithText:title parentView:parentView completion:completion];
}


@end

